//
// Created by fengzijie on 14-5-28.
//
// 
//


#import <AdSupport/AdSupport.h>
#import "EDJHttpClient.h"
#import "AFNetworkActivityIndicatorManager.h"
#import "OpenUDID.h"

#define BASE_URL        @"http://api.edaijia.cn"

static EDJHttpClient *_instance = nil;

@interface EDJHttpClient ()

@property (nonatomic, strong,readwrite) NSOperationQueue *operationQueue;

@end

@implementation EDJHttpClient {
    
    
}

@synthesize operationQueue = _operationQueue;

+ (EDJHttpClient *)instance {

    @synchronized (self) {
        if (_instance == nil) {
            NSURL *url = [NSURL URLWithString:BASE_URL];
            _instance = [[self alloc] initWithBaseURL:url];
        }
    }

    return _instance;
}

+ (void)destroy
{
    _instance = nil;
}

- (id)initWithBaseURL:(NSURL *)url {
    if ((self = [super initWithBaseURL:url])) {
        [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
        self.operationQueue = [[NSOperationQueue alloc] init];
        [self.operationQueue setMaxConcurrentOperationCount:NSOperationQueueDefaultMaxConcurrentOperationCount];
    }
    return self;
}

- (NSMutableURLRequest *)requestWithMethod:(NSString *)method path:(NSString *)path
                                parameters:(NSDictionary *)parameters {
    if (parameters) {
        if ([method isEqualToString:@"GET"] || [method isEqualToString:@"HEAD"] ||
                [method isEqualToString:@"POST"] || [method isEqualToString:@"DELETE"]) {
 //           NSMutableDictionary *mutableParameters = [parameters mutableCopy];

//            NSUUID *advertisingIdentifier = nil;
//            if (floor(NSFoundationVersionNumber) >= NSFoundationVersionNumber_iOS_6_1) {
//                [mutableParameters setValue:[[[NSUUID UUID] UUIDString] lowercaseString]
//                                     forKey:@"nonce"];
//                advertisingIdentifier =
//                        [[ASIdentifierManager sharedManager] advertisingIdentifier];
//            }
//            if (advertisingIdentifier)
//                [mutableParameters setValue:[[advertisingIdentifier UUIDString] lowercaseString]
//                                     forKey:@"advertising_identifier"];
//
//            parameters = [NSDictionary dictionaryWithDictionary:mutableParameters];
        }
    }
    
    NSString *urlString = [NSString stringWithFormat:@"%@%@",self.baseURL.absoluteString,path];
    NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:method URLString:urlString parameters:parameters error:nil];

    NSLog(@"req :%@ , body:%@", request, [[NSString alloc] initWithData:request.HTTPBody encoding:NSUTF8StringEncoding]);
    return request;
}

@end
